home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / importSkinMap.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  7.0 KB  |  251 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Procedure: importSkinMap
  19. //
  20. //    Description:
  21. //        This mel script is used to import a weight map for the skinning
  22. //        data for each joint that affects the selected skin.
  23. //
  24. //        To use, select the skin or skins, and type "importSkinMap"
  25. //
  26. //
  27.  
  28. proc int checkForLattice(string $obj) {
  29.     
  30.     string $cpShape[] = `ls -type controlPoint $obj`;
  31.     int $isLattice = 0;
  32.     if (size($cpShape) > 0) {
  33.         if (nodeType($obj) == "lattice") {
  34.             $isLattice = 1;
  35.         }
  36.     } else {
  37.         $cpShape = `listRelatives -pa -type controlPoint $obj`;
  38.         for ($cp in $cpShape) {
  39.             if (nodeType($cp) == "lattice") {
  40.                 $isLattice = 1;
  41.                 break;
  42.             }
  43.         }
  44.     }
  45.     if ($isLattice) {
  46.         string $warnMsg = ("Skipping "+$obj+": Lattice maps are not supported by Artisan.");
  47.         warning($warnMsg);
  48.     }
  49.     return $isLattice;
  50. }
  51.  
  52. global proc int importSkinWeightMap(string $fileName, string $fileType)
  53. {
  54.     if (`about -evalVersion`){
  55.         // Because fopen and fprint are disabled in PLE, 
  56.         // importSkinWeightMap is not supported.
  57.         warning ("importSkinWeightMap is not supported in Maya PLE.");
  58.         return 0;
  59.     }
  60.  
  61.     source "artAttrSkinCallback.mel";
  62.     source "artAttrSkinJointMenu.mel";
  63.  
  64.     // Make sure we are in Skin Paint Weight Tool.
  65.      string $currCtx = `currentCtx`;
  66.     artAttrSkinToolScript 4;
  67.  
  68.     string $artCmd = "artAttrCtx -query -exportfiletype `currentCtx`";
  69.     $fileType = eval( $artCmd );
  70.  
  71.     // Get the selected skins.
  72.     int $skinCount = 0;
  73.     int $latticeFound = 0;
  74.     string $currentSelection[] = `ls -sl`;
  75.     string $skins[];
  76.     string $clusters[];
  77.     for ($sel in $currentSelection) {
  78.         if (checkForLattice($sel)) {
  79.             $latticeFound = 1;
  80.             continue;
  81.         }
  82.         
  83.         // Find the cluster.
  84.         string $buff[];
  85.         tokenize($sel,".",$buff);
  86.         string $cluster = findRelatedSkinCluster($buff[0]);
  87.         if ("" != $cluster) {
  88.             $skins[$skinCount] = $sel;
  89.             $clusters[$skinCount] = $cluster;
  90.             $skinCount++;
  91.         }
  92.     }
  93.  
  94.     if (0 == $skinCount) {
  95.         if ($latticeFound) {
  96.             error("Lattice maps are not supported by Artisan.");
  97.         } else {
  98.             error("No skins were selected.");
  99.         }
  100.         return 0;
  101.     }
  102.  
  103.     $fileId = fopen( $fileName, "r" );
  104.     if ($fileId == 0) {
  105.         error("Unable to open the file: " + $fileName);
  106.         return 0;
  107.     }
  108.  
  109.     int $passed = 1;
  110.  
  111.     // Find the file and directory name.
  112.     string $buff[];
  113.     tokenize($fileName,"/",$buff);
  114.     string $fn = $buff[size($buff)-1];
  115.     tokenize($fn,"\\",$buff);
  116.     $fn = $buff[size($buff)-1];    
  117.     $dir = substring($fileName,1,size($fileName)-size($fn));
  118.     tokenize($fn,".",$buff);
  119.     $fn = $buff[0];
  120.     $dir += $fn;
  121.  
  122.     // Read in the master file that links the joint 
  123.     // names to the map names.
  124.     string $skinNames[];
  125.     string $jointNames[];
  126.     string $fileNames[];
  127.     string $clusterNames[];
  128.     string $skinNamesForC[];    
  129.     int $lineCount = 0;
  130.     
  131.     string $nextLine = `fgetline $fileId`;
  132.     while ( size( $nextLine ) > 0 ) {
  133.         int $numTokens = tokenize($nextLine,$buff);
  134.         if ($numTokens == 3) {
  135.             $skinNames[$lineCount] = $buff[0];
  136.             $jointNames[$lineCount] = $buff[1];
  137.             $fileNames[$lineCount] = $buff[2];
  138.             if (`objExists $buff[0]`) {
  139.                 string $relCluster = findRelatedSkinCluster($buff[0]);
  140.                 if ($relCluster != "") {
  141.                     int $found = 0;
  142.                     for ($clusterN in $clusterNames) {
  143.                         if ($clusterN == $relCluster) {
  144.                             $found = 1;
  145.                             break;
  146.                         }
  147.                     }
  148.                     if (! $found) {
  149.                         int $which = size($clusterNames);
  150.                         $clusterNames[$which] = $relCluster;
  151.                         $skinNamesForC[$which] = $buff[0];
  152.                     }
  153.                 }
  154.             }
  155.             $lineCount++;
  156.         }
  157.         $nextLine = `fgetline $fileId`;
  158.     }
  159.     if (catch(`fclose $fileId`)) {
  160.         error("Unable to close the file: " + $fileName);
  161.         $passed = 0;
  162.     }
  163.  
  164.     // Import the maps for each joint.
  165.      if ($passed) {
  166.  
  167.         int $counter;
  168.         int $oldNormWeights[];
  169.          for ($counter = 0; $counter < size($clusterNames); $counter++) {
  170.             $oldNormWeights[$counter] = `getAttr ($clusterNames[$counter]+".normalizeWeights")`;
  171.             setAttr ($clusterNames[$counter]+".normalizeWeights") 0;
  172.             // set all weights to zero
  173.             //
  174.             skinPercent -prw 1.1 $clusterNames[$counter];
  175.         }
  176.             
  177.         // Set the paint operation to 'replace'.
  178.         artAttrCtx -e -selectedattroper "absolute" `currentCtx`;
  179.  
  180.          $skinCount = 0;
  181.  
  182.          for ($counter = 0; $counter < $lineCount; $counter++) {
  183.             string $mapFn = ($dir+"/"+$fileNames[$counter]);
  184.             string $jt = $jointNames[$counter];
  185.             string $skin = $skinNames[$counter];
  186.             if (`file -q -ex $mapFn`) {
  187.                 if (`objExists $jt` && `objExists $skin`) {
  188.  
  189.                     // Make the joint a selected node.
  190.                     string $shortName = artAttrSkinShortName($jt);
  191.                     select -r $skin;
  192.                     artSkinSelectInfluence( "artAttrCtx", $jt, $shortName );
  193.  
  194.                     // Get the cluster name.
  195.                     $artCmd = "artAttrCtx -q -objattrArray  `currentCtx`";
  196.                     string $selectedAttr = eval( $artCmd );
  197.  
  198.                     string $tmpName[];
  199.                     tokenize( $selectedAttr, ".", $tmpName );    
  200.                     if ( size($tmpName) < 2 ) 
  201.                         continue;
  202.  
  203.                     string $cluster = $tmpName[1];
  204.  
  205.                     print("// Importing "+$fileNames[$counter]+" to ");
  206.                     print($skin+" for joint "+$jt+"\n");
  207.  
  208.                     // Import the attribute map now.
  209.                     artAttrCtx -e -importfileload $mapFn `currentCtx`;
  210.                     getAttr -silent ($cluster+".og[0]");
  211.                 } else {
  212.                     if (! `objExists $jt`) {
  213.                         print("// Skipping "+$jt+": joint does not exist.\n");
  214.                     } else {
  215.                         print("// Skipping "+$skin+": skin does not exist.\n");                            
  216.                     }
  217.                 }
  218.             } else {
  219.                 print("// Skipping "+$mapFn+": File not found.\n");
  220.             }
  221.         }
  222.  
  223.          for ($counter = 0; $counter < size($clusterNames); $counter++) {
  224.             
  225.             setAttr ($clusterNames[$counter]+".normalizeWeights") $oldNormWeights[$counter];
  226.             select -r $skinNamesForC[$counter];
  227.             skinPercent -normalize true $clusterNames[$counter];
  228.         }
  229.      }
  230.  
  231.     // Go back to the original context.
  232.      setToolTo $currCtx;
  233.     select -r $currentSelection;
  234.     
  235.     return $passed;
  236. }
  237.  
  238. global proc importSkinMap( string $version, string $args[] ) {
  239.  
  240.     // set the fileBrowser directory to the images dir if it exists
  241.     //
  242.     string $imageDir = (`workspace -q -rd`+"sourceimages\/");
  243.     if (`file -q -ex $imageDir`) {
  244.         workspace -dir $imageDir;
  245.     }
  246.  
  247.     // bring up the file browser dialog so that they can choose a file name
  248.     //
  249.     fileBrowser("importSkinWeightMap","Import Map","map",0);
  250. }
  251.